package hn.cch.spring.aop.proxy.demo;

import hn.cch.spring.aop.aspect.MethodInterceptorImpl;
import hn.cch.spring.aop.proxy.ProxyApi;
import hn.cch.spring.aop.proxy.ProxyImpl;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;

public class JdkProxyDemo {

    public static void main(String[] args) {
         // Spring AOP实现过程，测试使用接口实现类作为代理目标，Spring AOP使用JDK代理实现
         AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("execution(* method(*))");
        MethodInterceptor advice = new MethodInterceptorImpl();
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.addAdvisor(advisor);
        proxyFactory.setTarget(new ProxyImpl());
        proxyFactory.setInterfaces(ProxyApi.class); // 设置目标实现接口，使用JDK代理
        ProxyApi proxyApi = (ProxyApi) proxyFactory.getProxy();
        System.out.println(proxyApi.getClass());
        proxyApi.method("xyz");
    }

}
